<?php

namespace Biz\service\brand;

use Biz\model\brand\BrandScore as mBrandScore;
use Biz\model\brand\BrandScoreLog as mBrandScoreLog;
use Biz\cache\brand\BrandScoreCache;
use Biz\model\brand\Brand as mBrand;
use Biz\service\BrandRights\BrandLevelRightsService;
use Doraemon\model\Task\TaskBase as mTaskBase;
use Doraemon\tools\Log;

/**
 *
 * Class BrandScore
 * @package Biz\service\brand
 */
class BrandScore
{

    /**
     * 任务奖励品牌指数
     * @param $uid
     * @param $taskInfo
     * @return bool
     */
    public static function awardFromTask($uid, $taskInfo)
    {
        try {
            $taskTime = $taskInfo['task_time'];
            $taskId = $taskInfo['task_id'];
            $groupId = $taskInfo['group_id'];
            //品牌主页使用，和大数据同级品牌指数区别
            $brandInfo = mBrand::getBrandInfoByUid($uid);
            $brandId = $brandInfo['id'] ?? 0;
            $initialGrowth = $brandInfo['growth_value'] ?? 0;
            $initialLevel = $brandInfo['level'] ?? 0;
            if (empty($brandId)) {
                throw new \Exception('未查询到当前用户的品牌信息');
            }
            $scoreInfo = mBrandScore::getInfoByBrandId($brandId);
            $taskRewardArr = $taskInfo['task_reward'];
            $groupRewardArr = $taskInfo['group_reward'];
            $awardScore = 0;
            $taskScore = 0;
            $groupScore = 0;
            $growth = 0;
            foreach ($taskRewardArr as $row) {
                if ($row['reward_type'] == mTaskBase::TASK_REWARD_BRAND_SCORE && $row['reward_value']) {
                    $awardScore += $row['reward_value'];
                    $taskScore += $row['reward_value'];
                }
                if ($row['reward_type'] == mTaskBase::TASK_REWARD_LEVEL && $row['reward_value']) {
                    $growth += $row['reward_value'];
                }
            }
            foreach ($groupRewardArr as $row) {
                if ($row['reward_type'] == mTaskBase::TASK_REWARD_BRAND_SCORE && $row['reward_value']) {
                    $awardScore += $row['reward_value'];
                    $groupScore += $row['reward_value'];
                }
            }
            if ($awardScore > 0) {
                //查询品牌指数
                $brandScore = BrandScoreCache::getBrandScore($brandId);
                $brandScore = !empty($brandScore) ? $brandScore : 0;
                if (!empty($scoreInfo)) {
                    $score = $scoreInfo['score'];
                    $score = ($brandScore > $score) ? $brandScore : $score;
                    $score = $awardScore + $score;
                    mBrandScore::updateData(["score" => $score], ['brand_id' => $brandId]);
                } else {
                    $score = $awardScore + $brandScore;
                    mBrandScore::insertData(['brand_id' => $brandId, 'score' => $score]);
                }
                if ($taskScore) {
                    $taskObjId = self::getTaskObjId($groupId, $taskId, $taskTime);
                    mBrandScoreLog::insertData([
                        'brand_id' => $brandId,
                        'score' => $taskScore,
                        'source' => mBrandScoreLog::SOURCE_BRAND_TASK,
                        'obj_id' => $taskObjId,
                    ]);
                }
                if ($groupScore) {
                    $groupObjId = self::getGroupObjId($groupId, $taskTime);
                    mBrandScoreLog::insertData([
                        'brand_id' => $brandId,
                        'score' => $groupScore,
                        'source' => mBrandScoreLog::SOURCE_BRAND_TASK,
                        'obj_id' => $groupObjId,
                    ]);
                }
            }

            //新增商家成长值
            if ($growth > 0) {
                $level = BrandGrowth::growthLevel($initialGrowth + $growth);
                mBrand::incrGrowth($brandId, $growth, $level);
                BrandGrowth::growthLog($brandId, $uid, $initialLevel, $level, $groupId, $taskId, $growth);
                //升级之后增加品牌权益
                 if ($level > $initialLevel) {
                     BrandLevelRightsService::afterLevelUp($brandId, $level, $initialLevel);
                     BrandGrowth::upgrade($brandId, $uid, $level);
                 }
            }
        } catch (\Exception $e) {
            Log::debug('任务奖励品牌指数异常', ['err_code' => $e->getCode(), 'err_msg' => $e->getMessage(), 'uid' => $uid, 'task_info' => $taskInfo]);
            return false;
        }
        return true;
    }

    public static function getTaskObjId($groupId, $taskId, $taskTime)
    {
        return 'task' . '_' . $groupId . '_' . $taskId . '_' . $taskTime;
    }

    public static function getGroupObjId($groupId, $taskTime)
    {
        return 'group' . '_' . $groupId . '_0_' . $taskTime;
    }
}